package com.zx.mes.hyl.security.core.authentication;

import com.zx.mes.hyl.security.core.properties.SecurityConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

/**
 * 表单登录配置
 * @author huayunlong
 * @date 2018-6-3
 */
@Component
public class FormAuthenticationConfig {

    @Autowired
    private AuthenticationSuccessHandler longAuthenticationSuccessHandler;

    @Autowired
    private AuthenticationFailureHandler longAuthenticationFailureHandler;


    public void configure(HttpSecurity http) throws Exception {
        http
            /* 表单登录 另一种:httpBasic **/
                .formLogin()
            /* 登录页面 **/
                .loginPage(SecurityConstants.DEFAULT_UN_AUTHENTICATION_URL)
                .loginProcessingUrl(SecurityConstants.DEFAULT_SIGN_IN_PROCESSING_URL_FORM)
            /* 成功处理器 **/
                .successHandler(longAuthenticationSuccessHandler)
            /* 失败处理器 **/
                .failureHandler(longAuthenticationFailureHandler);
    }
}
